package com.doudou.base.resources.controller;

import com.doudou.base.resources.entity.dto.user.realinfo.UserRealInfoAddDto;
import com.doudou.base.resources.entity.dto.user.realinfo.UserRealInfoUpdateDto;
import com.doudou.base.resources.service.UserRealInfoService;
import com.doudou.client.entity.UserRealInfoDto;
import com.doudou.commons.entity.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户的真实信息  前端控制器
 * @author 傻男人
 * @date 2020-05-14
 */
@Slf4j
@RestController
@RequestMapping("/base/user")
@Api(value = "用户的真实信息信息",tags = "用户的真实信息信息")
public class UserRealInfoController {

    @Autowired
    protected UserRealInfoService userRealInfoService;

    /**
     * 添加用户的真实信息
     * @param userRealInfoAddDto
     * @param result
     * @return
     */
    @ApiOperation(value = "添加用户的真实信息",notes = "添加用户的真实信息")
    @PostMapping(value = "/real/info")
    public Result addUserRealInfo(@Valid @RequestBody @ApiParam(value = "用户的真实信息添加dto",required = true) UserRealInfoAddDto userRealInfoAddDto, BindingResult result) {
        if (result.hasErrors()) {
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        UserRealInfoDto userRealInfoDto = userRealInfoService.addUserRealInfo(userRealInfoAddDto);
        return Result.ok(userRealInfoDto);
    }

    /**
     * 根据ids删除用户的真实信息
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据ids删除用户的真实信息",notes = "根据ids删除用户的真实信息")
    @ApiImplicitParam(name = "ids",value="用户的真实信息ids",dataType="String",required = true,paramType = "path")
    @DeleteMapping(value = "/real/info/{ids}")
    public Result deleteUserRealInfo(@PathVariable("ids") String ids) {
        Integer number = userRealInfoService.deleteUserRealInfo(ids);
        return Result.ok(number);
    }

    /**
     * 根据id修改用户的真实信息
     * @param id
     * @param userRealInfoUpdateDto
     * @param result
     * @return
     */
    @ApiOperation(value = "根据id修改用户的真实信息",notes = "修改用户的真实信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value="用户的真实信息id",dataType="Long",required = true,paramType = "path")})
    @PutMapping(value = "/real/info/{id}")
    public Result updateUserRealInfo(@PathVariable("id")  Long id,@Valid @RequestBody @ApiParam(value = "用户的真实信息修改dto",required = true) UserRealInfoUpdateDto userRealInfoUpdateDto, BindingResult result) {
        if (result.hasErrors()) {
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        userRealInfoUpdateDto.setId(id);
        Integer sum = userRealInfoService.updateUserRealInfo(userRealInfoUpdateDto);
        return Result.ok(sum);
    }

    /**
     * 根据ids查询用户的真实信息
     * @param ids
     * @return
     */
    @ApiOperation(value = "根据ids查询用户的真实信息",notes = "根据ids查询用户的真实信息")
    @ApiImplicitParam(name = "ids",value="用户的真实信息ids",dataType="String",required = true,paramType = "path")
    @GetMapping(value = "/real/info/{ids}")
    public Result findUserRealInfo(@PathVariable("ids")  String ids) {
        List<UserRealInfoDto> list = userRealInfoService.findUserRealInfo(ids);
        return Result.ok(list);
    }

    /**
    * 根据id查询用户的真实信息
    * @param primaryId   用户的真实信息的id
    * @return
    */
    @ApiOperation(value = "根据id查询用户的真实信息",notes = "根据id查询用户的真实信息")
    @ApiImplicitParam(name = "primaryId",value="用户的真实信息的id",dataType="Long",required = true,paramType = "path")
    @GetMapping(value = "/real/info/{primaryId}/primaryId")
    public Result findUserRealInfo(@PathVariable("primaryId")Long primaryId) {
        UserRealInfoDto result = userRealInfoService.findUserRealInfoById(primaryId);
        return Result.ok(result);
    }
}
